package com.htandroid.htandroid.aty;

/**
 * Created by Administrator on 2015/12/26.
 */

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.fragment.AbDialogFragment;
import com.ab.fragment.AbLoadDialogFragment;
import com.ab.model.AbMenuItem;
import com.ab.soap.AbSoapUtil;
import com.ab.util.AbDialogUtil;
import com.ab.util.AbToastUtil;
import com.ab.view.ioc.AbIocView;
import com.ab.view.titlebar.AbTitleBar;
import com.htandroid.htandroid.R;
import com.htandroid.htandroid.adapter.ListPopAdapter;
import com.htandroid.htandroid.entity.GcwxList;
import com.htandroid.htandroid.global.MyApplication;
import com.htandroid.htandroid.sys.Basenvo;
import com.htandroid.htandroid.sys.InstorageURL;
import com.htandroid.htandroid.sys.User;
import com.htandroid.htandroid.webservice.BuildGcwxXMLService;
import com.htandroid.htandroid.webservice.XMLGcwxVouchlist;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.htandroid.htandroid.constance.ConstanceServer.BaseUrl_New;

public class Gcwx_yh_detail extends AbActivity {
    public MyApplication myApplication;
    private GcwxList vouch;
    private ArrayList<GcwxList> vouchlist;
    private String initTime = "";
    private String listtype = "0";
    private int icurrentrow;

    private AbTitleBar mAbTitleBar = null;
    private View rightView1 = null;
    private View rightView2 = null;
    private View rightView3 = null;
    private TextView tv_currentrow;

    private AbLoadDialogFragment mDialogFragment = null;
    private PopupWindow popupWindow;
    private ListView popListView;
    private View popView;

    @AbIocView(id = R.id.btn1_tv, click = "btnClick")
    TextView btn1_tv;
    @AbIocView(id = R.id.csuspendreason_et)
    EditText csuspendreason_et;
    @AbIocView(id = R.id.csuspender_et)
    TextView csuspender_et;
    @AbIocView(id = R.id.dsuspenddate_et)
    TextView dsuspenddate_et;
    @AbIocView(id = R.id.cvouchid_tv)
    TextView cvouchid_tv;
    @AbIocView(id = R.id.cdepname_tv)
    TextView cdepname_tv;
    @AbIocView(id = R.id.cbxtext_tv)
    TextView cbxtext_tv;
    @AbIocView(id = R.id.ddate_tv)
    TextView ddate_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.layout_gcwx_yh_detail);
        myApplication = (MyApplication) abApplication;
        initview();
    }

    public void btnClick(final View v) {
        mDialogFragment = AbDialogUtil.showLoadDialog(this, R.layout.view_dialog_loading, "正在加载...");
        mDialogFragment.setAbDialogOnLoadListener(new AbDialogFragment.AbDialogOnLoadListener() {

            @Override
            public void onLoad() {
                btn1_click(v);
            }
        });
    }

    //初始化
    private void initview() {
        mAbTitleBar = this.getTitleBar();
        mAbTitleBar.setTitleText("维修延缓");
        mAbTitleBar.setLogo(R.drawable.icon_back);
        mAbTitleBar.getLogoView().setLayoutParams(new LinearLayout.LayoutParams(58, 57));
        mAbTitleBar.getLogoView().setFocusable(true);
        mAbTitleBar.getLogoView().setFocusableInTouchMode(true);
        mAbTitleBar.setTitleBarBackground(R.color.common_title);
        mAbTitleBar.setTitlePadding(118, 0, 0, 0);

        mAbTitleBar.clearRightView();
        rightView1 = mInflater.inflate(R.layout.btn_abs_back, null);
        mAbTitleBar.addRightView(rightView1);
        rightView2 = mInflater.inflate(R.layout.tv_currentrow, null);
        mAbTitleBar.addRightView(rightView2);
        rightView3 = mInflater.inflate(R.layout.btn_abs_forward, null);
        mAbTitleBar.addRightView(rightView3);
        tv_currentrow = (TextView) rightView2.findViewById(R.id.tv_currentrow);

        //获取传递的对象参数
        Intent intent = getIntent();
        vouchlist = (ArrayList<GcwxList>) intent.getSerializableExtra("vouchlist");
        listtype = intent.getExtras().getString("listtype");
        icurrentrow = getIntent().getExtras().getInt("currentrow");

        insert_data(icurrentrow);

        //弹出窗口
        popView = mInflater.inflate(R.layout.list_pop, null);
        popListView = (ListView) popView.findViewById(R.id.pop_list);
        popupWindow = new PopupWindow(popView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        AnimationSet animationSet = new AnimationSet(true);
        ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 1, 1, Animation.ZORDER_BOTTOM, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animationSet.setDuration(250);
        animationSet.addAnimation(scaleAnimation);
        popView.startAnimation(animationSet);
        ImageView more_close = (ImageView) popView.findViewById(R.id.item_close);
        more_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });
    }

    //为窗口填充数据
    public void insert_data(int row) {
        vouch = vouchlist.get(row);
        icurrentrow = row;
        tv_currentrow.setText((icurrentrow + 1) + "/" + (vouchlist.size()));
        //接单和取消接单
        showwin(true);
        //填写文本内容
        cvouchid_tv.setText(vouch.getCvouchid());

        if (!vouch.getCdepcode().equals("")) {
            cdepname_tv.setText(vouch.getCdepname());
            cdepname_tv.setTag(vouch.getCdepcode());
        }

        cbxtext_tv.setText(vouch.getCbxtext());
        ddate_tv.setText(Basenvo.customDateFormat("yyyy-MM-dd HH:mm", vouch.getDdate()));

        Date curDate = new Date(System.currentTimeMillis());
        //延缓时间
        if (!vouch.getDsuspenddate().equals("")) {
            initTime = Basenvo.customDateFormat("yyyy-MM-dd HH:mm", vouch.getDsuspenddate());
        } else {
            initTime = Basenvo.customDateFormat("yyyy-MM-dd HH:mm", curDate);
        }
        dsuspenddate_et.setText(initTime);
        //
        if (!Basenvo.equal(vouch.getCsuspender(), "")) {
            csuspender_et.setText(vouch.getCsuspender());
        } else {
            csuspender_et.setText("");
        }
        //结论
        if (!Basenvo.equal(vouch.getCsuspendreason(), "")) {
            csuspendreason_et.setText(vouch.getCsuspendreason());
        } else {
            csuspendreason_et.setText("");
        }
        //写入表头数据
        insert_data_title();
    }

    public void insert_data_title() {
        //表头信息显示弹窗
        ImageView listmore_iv = (ImageView) findViewById(R.id.titlemore_iv);
        listmore_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<Map<AbMenuItem, AbMenuItem>> data = new ArrayList<Map<AbMenuItem, AbMenuItem>>();
                final ImageView imgs = (ImageView) v;
                Map<AbMenuItem, AbMenuItem> map1 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map2 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map3 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map4 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map5 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map6 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map7 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map8 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map9 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map10 = new HashMap<AbMenuItem, AbMenuItem>();
                Map<AbMenuItem, AbMenuItem> map11 = new HashMap<AbMenuItem, AbMenuItem>();
                map1.put(new AbMenuItem("　　报修区域："), new AbMenuItem(vouch.getCwxplname()));
                map2.put(new AbMenuItem("　　报修等级："), new AbMenuItem(vouch.getCdjname()));
                map3.put(new AbMenuItem("　　报修类型："), new AbMenuItem(vouch.getCtypename()));
                map4.put(new AbMenuItem("　　维修类别："), new AbMenuItem(""));
                map5.put(new AbMenuItem("　　维修工分："), new AbMenuItem(""));
                map6.put(new AbMenuItem("　　　报修人："), new AbMenuItem(vouch.getCmaker()));
                map7.put(new AbMenuItem("预计维修时间："), new AbMenuItem(Basenvo.customDateFormat("yyyy-MM-dd HH:mm", vouch.getEswxdate())));
                map8.put(new AbMenuItem("　　　接单人："), new AbMenuItem(vouch.getCjder()));
                map9.put(new AbMenuItem("　　　维修组："), new AbMenuItem(vouch.getCwxgcode()));
                map10.put(new AbMenuItem("　　　维修人："), new AbMenuItem(vouch.getCwxpcode()));
                map11.put(new AbMenuItem("协助维修人："), new AbMenuItem(vouch.getXzwxnames()));
                data.add(map1);
                data.add(map2);
                data.add(map3);
                data.add(map4);
                data.add(map5);
                data.add(map6);
                data.add(map7);
                data.add(map8);
                data.add(map9);
                data.add(map10);
                data.add(map11);
                ListPopAdapter mListPopAdapter = new ListPopAdapter(Gcwx_yh_detail.this, data, R.layout.list_popitem);
                popListView.setAdapter(mListPopAdapter);
                //弹出窗口后,变更更多图标
                imgs.setImageResource(R.drawable.item_more_choose);
                popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    public void onDismiss() {
                        //还原图标
                        imgs.setImageResource(R.drawable.item_more);
                    }
                });
                popupWindow.showAsDropDown(v);
            }
        });
    }

    public void showwin(boolean stype) {
        //背景变白，字变黑色
        Integer backgroundcolor = Color.TRANSPARENT;
        csuspendreason_et.setEnabled(stype);
        if (stype) {
            csuspendreason_et.setBackgroundResource(R.drawable.edit_background);
        } else {
            csuspendreason_et.setBackgroundColor(backgroundcolor);
        }
    }

    //维修信息保存
    public void btn1_click(View v) {
        if (csuspendreason_et.getText() == null || csuspendreason_et.getText().toString().equals("")) {
            AbToastUtil.showToastInThread(getApplicationContext(), "必须输入延缓说明!");
            AbDialogUtil.removeDialog(Gcwx_yh_detail.this);
            return;
        }
        //写入延缓说明
        vouch.setCsuspendreason(csuspendreason_et.getText().toString());

        //写入延缓时间
        vouch.setDsuspenddate(dsuspenddate_et.getText().toString());

        String urlString = "http://" + InstorageURL.SERVERURL+ BaseUrl_New;
        String[] parms = {"username", "cacc_id", "iyear"};
        String[] vals = {User.nickName, (String) myApplication.getGs_ztid(), (String) myApplication.getGs_year()};
        //生成xml
        BuildGcwxXMLService service = new BuildGcwxXMLService();
        String xmlstring = service.buildXML("gcwx_yhsave", InstorageURL.NAME_SPACE, parms, vals, "in" + String.valueOf(parms.length), vouch, null, null, null, null, null, null);
        mAbSoapUtil.xmlCall(urlString, InstorageURL.NAME_SPACE, "gcwx_yhsave", xmlstring, new AbSoapUtil.WebServiceCallBack() {
            @Override
            public void onSucced(String result) {
                if (result != null) {
                    //解析xml文件,返回列表
                    List<GcwxList> vouchlist = XMLGcwxVouchlist.paraseCommentInfors(result);
                    //默认显示第一行数据
                    if (vouchlist.size() > 0) {
                        vouch = vouchlist.get(0);
                    }
                    if (vouch.getSucstatus().equals("0")) {
                        AbToastUtil.showToastInThread(getApplicationContext(), "单据保存失败!");
                    } else {
                        Intent intent = new Intent();
                        setResult(R.layout.layout_gcwx_xd_detail / 100000, intent);  //设置返回结果
                        finish();
                    }
                } else {
                    AbToastUtil.showToastInThread(getApplicationContext(), "单据保存失败!数据库错误");
                }
                AbDialogUtil.removeDialog(Gcwx_yh_detail.this);
            }

            @Override
            public void onFailure(String result) {
                AbDialogUtil.removeDialog(Gcwx_yh_detail.this);
                AbToastUtil.showToastInThread(getApplicationContext(), "单据保存失败!数据库错误");
            }
        });
    }

    //向左翻页
    public void btn_abs_back(View v) {
        if (icurrentrow == 0) {
            AbToastUtil.showToastInThread(getApplicationContext(), "已经是第一项");
        } else {
            insert_data(icurrentrow - 1);
        }
    }

    //向右翻页
    public void btn_abs_forward(View v) {
        if (icurrentrow == (vouchlist.size() - 1)) {
            AbToastUtil.showToastInThread(getApplicationContext(), "已经是最后一项");
        } else {
            insert_data(icurrentrow + 1);
        }
    }
}

